package com.yc.testThread;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 功能：
 * 依次起两个线程，两个线程功能一样：
 * 		创建文件，然后对新建对文件进行处理
 * @author yc
 *【本demo需要使用logback】
 */
public class TT {
	
	public static void main(String[] args) {
		//这里替换上本地的一个目录，用来跑本程序。注意：因为程序会对该目录进行清理，一定要写一个没有用的 或者 不存在的目录！！！
		String dirPath = "/Users/yc/yctemp/abc";
		Thread A = new Thread(new myRunnable1(dirPath));
		A.setName("A");
		Thread B = new Thread(new myRunnable1(dirPath));
		B.setName("B");
		A.start();//启动A线程
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		B.start();//启动B线程
	}

}

//测试线程
class myRunnable1 implements Runnable {
	private final static Logger log = LoggerFactory.getLogger(myRunnable1.class);
	TEntity runState = new TEntity();//用来记录运行状态
	private String dirPath;
	public myRunnable1(String dirPath) {
		this.dirPath = dirPath;
	}
	
	//删除目录以及目录下面的所有文件
	public boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
	}
	
	@Override
	public void run() {
		log.info("myRunnable1开始");
		
		//删除目录
		deleteDir(new File(dirPath));
		//创建目录，文件
		File f = new File(dirPath);
		f.mkdirs();
		//在目录中创建1个文件
		String filePath = dirPath + File.separator + System.currentTimeMillis();
		log.info("线程" + Thread.currentThread().getName() + "创建文件：" + filePath);
		File newFile = new File(filePath);
		try {
			newFile.createNewFile();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		//处理文件
		FileInputStream itemIs = null;
		for (int i = 1; i <= 20; i++) {
			try {
				itemIs = new FileInputStream(newFile);
				log.info("第" + i + "次处理文件。。");
				Thread.sleep(1000);
				runState.setName("第" + i + "次处理成功");
			} catch (Exception e) {
				runState.setName("第" + i + "次处理失败");
				runState.getErrorMsg().add(e.getMessage());//把报错信息存进
				log.error("处理文件失败", e);
			} finally {
				if (itemIs != null) {
					try {
						itemIs.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
		
		log.info("myRunnable1结束");
	}
	
}

//测试实体类(用于保存错误信息)
class TEntity {
	private Integer id;
	private String name;
	private List<String> errorMsg;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<String> getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(List<String> errorMsg) {
		this.errorMsg = errorMsg;
	}
}